package main

import (
	"fmt"
	"os"

	"email/config"
	"email/contract"
	"email/enum"
	"email/service"

	"github.com/spf13/viper"
)

var conf config.Config

func main() {
	if err := initConf(); err != nil {
		panic(err)
	}

	input := &contract.ReqInput{
		Attachments: []*contract.Attachment{
			{
				FileName: "example.txt",
				Content:  "dGhpcyBpcyBhIGV4YW1wbGU=",
			},
			{
				FileName: "example.txt",
				Content:  "dGhpcyBpcyBhIGV4YW1wbGU=",
			},
		},
		Destination: []string{
			"angelia@163.com",
			"hecc@163.com",
		},
		FromEmailAddress: "QCLOUDTEAM &ltnoreply@mail.qcloud.com&gt",
		ReplyToAddresses: "noreply@mail.qcloud.com",
		Subject:          "YourTestSubject",
		TemplateData:     "{\"name\":\"xxx\",\"age\":\"xx\"}",
		TemplateID:       enum.NotificationMail,
		TriggerType:      "1",
		Unsubscribe:      "1",
	}

	mailSvc := service.NewMailSvc(&conf.Ses)
	if err := mailSvc.Send(input); err != nil {
		return
	}
}

func initConf() error {
	wd, _ := os.Getwd()

	// 加载统一配置
	configPath := fmt.Sprintf("%s%s", wd, "/configfile/config.yaml")
	fmt.Println(configPath)

	v := viper.New()
	v.SetConfigFile(configPath)
	err := v.ReadInConfig()
	if err != nil {
		return err
	}

	// 加载配置
	if err = v.Unmarshal(&conf); err != nil {
		return err
	}
	fmt.Printf("\x1b[0;%dm%s\x1b[0m\n", 34, fmt.Sprintf("加载配置成功，配置是%+v", conf))
	return nil
}
